package cn.lcfms.app.admin.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.lcfms.app.admin.bean.CategoryBean;
import cn.lcfms.app.admin.service.CommonService;
import cn.lcfms.app.admin.service.DataService;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.bin.view.CommonEditView;
import cn.lcfms.bin.view.CommonTreeView;
import cn.lcfms.utils.TimeUtils;

@PermitPoll(type="CMS管理",name="栏目管理")
@Controller("admin.CategoryController")
@RequestMapping("/admin/category")
public class CategoryController extends AdminBaseController{
	
	@Autowired
	private DataService g_service;
	
	@Autowired
	private CommonService c_service;
	
	@RequestMapping("/addCategory")
	public CommonEditView addCategory(){
		BaseService service=Application.getService("mark");
		CommonEditView view=new CommonEditView("增加栏目");
		view.setInputForm("栏目名称", "title", "", "unempty");	
		view.addObject("pTitle", "父");
		view.setViewForm("frame/categories.jsp");	
		List<HashMap<String, Object>> list = service.column("title").selectList();
		view.setCheckboxForm("标签", "labels", c_service.parseListToString("title", list), c_service.parseListToString("title", list), "");	
		view.setUploadImageForm("栏目图片","imgfile", null, 0, 10);
		view.setEditorForm("栏目描述", "desc", "");
		view.setFormAction("saveCategory");		
		return view;
	}
	
	@RequestMapping("/updateCategory")
	public CommonEditView updateCategory(int id){
		BaseService service=Application.getService("mark");
		List<HashMap<String, Object>> list = service.column("title").selectList();	
		service.setTable("categories");
		HashMap<String, Object> map = service.where("id=#{id}").setData(id).selectMap();
		CommonEditView view=new CommonEditView("修改栏目");
		view.setHiddenForm("id", id);
		view.setInputForm("栏目名称", "title", map.get("title"), "unempty");	
		view.addObject("pTitle", "父");
		int pid = (int) map.get("pid");
		HashMap<Integer, String> category = g_service.getCategory(pid);
		view.setViewForm("frame/categoriesEdit.jsp");		
		view.setCheckboxForm("标签", "labels", c_service.parseListToString("title", list), c_service.parseListToString("title", list),(String)map.get("labels"));
		view.addObject("categoryId", category.keySet());				
		JSONArray photo = g_service.getCategoryFile(id);		
		view.setUploadImageForm("栏目图片","imgfile", photo, 0, 10);
		view.setEditorForm("栏目描述", "desc", map.get("desc"));
		view.setFormAction("saveCategory");		
		return view;
	}
	
	@RequestMapping("saveCategory")
	public String saveCategory(CategoryBean cate,int[] category_ids,HttpServletRequest request){
		int topid=0;
		if(category_ids != null) {
			for(int i=0;i<category_ids.length;i++){			
				if(topid<category_ids[i]){
					topid=category_ids[i];
				}
			}
		}
		cate.setPid(topid);
		cate.setCreated_at(TimeUtils.getCurrentDateTime());
		g_service.setPhoto(request, cate, "imgfile");
		g_service.saveCategory(cate);
		return "{code:1,msg:保存成功！,url:catelist}";
	}
	
	@RequestMapping("/deteCategory")
	public String deteCategory(int id){			
		BaseService service=Application.getService("categories");
		service.deleteById(id);
		return "{code:1,msg:删除成功！,url:catelist}";
	}
	
	@RequestMapping(value="/getCategory",produces="text/html;charset=utf-8")
	@ResponseBody
	public String getCategory(int id){			
		BaseService service=Application.getService("categories");
		service.where("pid=#{pid}");
		if(id==0){
			service.setData("");
		}else{
			service.setData(id);
		}		
		service.column("id","title");
		List<HashMap<String, Object>> list = service.order("sort").selectList();
		JSONArray array=JSONArray.fromObject(list);
		return array.toString();
	}
	
	
	@RequestMapping("/catelist")
	public CommonTreeView catelist(HttpServletRequest request){
		BaseService service=Application.getService("categories","a");	
		service.column("a.id as ID","a.pid as pid");	
		service.column("a.title as 栏目名称");	
		if(null!=request.getParameter("id")){
			String id=request.getParameter("id");
			service.where("a.pid="+id);
		}
		service.order("a.sort");
		CommonTreeView view=new CommonTreeView("栏目管理",0,service);
		view.setId("ID");
		view.setPid("pid");
		view.setColumnWidth("栏目名称", 100);
		view.setColumnWidth("模板", 300);
		view.setToolBarUrl("添加栏目", "icon-jia", "addCategory");	
		view.setDeleteLayer("deteCategory?id=${ID}");
		view.setUpdateUrl("updateCategory?id=${ID}");		
		view.setEditTab("查看内容", "icon-tiaozhuan", request.getAttribute("APP")+"admin/article/list?cid=${ID}", 193);
		view.setSaveSortAction("saveTypeSort");
		return view;		
	}
	
	@RequestMapping(value="/saveTypeSort",produces="text/html;charset=utf-8")
	@ResponseBody
	public String saveTypeSort(String data) {
		BaseService service = Application.getService(CategoryBean.class);
		JSONArray array=JSONArray.fromObject(data);
		for(int i=0;i<array.size();i++) {
			int cid = array.getJSONObject(i).getInt("cid");
			int pid = array.getJSONObject(i).getInt("pid");
			service.setData(pid,cid).where("id=#{id}").update("pid");
		}
		return "保存成功！";
	}
	
}
